
var app = getApp();
import Qs from "qs";

import util from "/common/js/util.js";

Page({
  data: {
    current: 2,
		tabs: [
					{name: '月排行', key: 2},
					{name: '年排行', key: 3}
				],
    totalType:2,  
    date_str:util.times("ym"),//日期字符  
    choose_date:"",
		list: [],
    basicVisible:false
  },
  onLoad() {
    
  },
  onShow() {
    this.getIsLogin();
  },
  onPullDownRefresh() {
    // 页面被下拉
    
    this.getRankingsFn();
  },
  tabChange(e){
    let index = e.currentTarget.dataset.index;
    let totalType= this.data.tabs[index].key
    this.setData({
      totalType
    })

    this.getRankingsFn();
  },

  complexChoose(e) {
    let now = util.getStringYMDByDate(new Date())
    let currentDate = this.data.date_str;
    let that = this;
    // yyyy-MM-dd yyyy-MM
    dd.datePicker({
      format: 'yyyy-MM',
      currentDate,
      success: (res) => {
      // console.log("--es.date", res.date);
       this.setData({
        date_str:res.date
       });
       this.getRankingsFn();

      },
    });
  },  
  onRoutNoteGroup(e){
    let index = e.currentTarget.dataset.index;
    let item= this.data.list[index];
    // console.log(item);
    // return
    let userId = item.userId;
    let userName = item.userName;
    
    let query={
      userId,userName
    }
    
    let p = Qs.stringify(query);
    let path = '/pages/note/noteList/noteList';
   //console.log("---p:",p);
    let url = `${path}?${p}`;
    dd.navigateTo({
      url: url
    })
  },
  getIsLogin(){
    let api = "/user/info/getUserInfo", list = { };
    app.$post(api, list).then(re => {
      try {
        if (re.code == 401) {
          app.authLoginFn().then(res=>{
            this.getRankingsFn();
          })
        }
        else {
          this.getRankingsFn();
        };
      } catch (error) {
      }
    }).catch(err => {
     
    });
  },
  getRankingsFn(){
    let api = "/common/getRankings";
    let totalType=this.data.totalType;
    let list = {
      totalType,
      date_str:this.data.date_str
    };
    
    let that = this;
    
    dd.showLoading({
        content: '数据获取中...',
      })
    app.$post(api, list).then(res => {
     dd.stopPullDownRefresh()
     dd.hideLoading();
      if (res.code == 200) {
        this.setData({
          "list":res.data
        })
        
      }else{
        util.message(res.message);

      }
    })
  },
  onShareAppMessage() {

  //  let p = Qs.stringify(this.data.queryData);
    let path = '/pages/ranking_list/ranking_list';
   // let url = `${path}?${p}`;
    return {
      title: '恒丽书城',
      desc: `排行版`,
      path: path,
    };
  },

  handleTapLink(){
    this.setData({
      basicVisible:true
    });
  },
  handleClose() {
    this.setData({
      basicVisible:false
    })
  }
});
